import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper_view/flutter_swiper_view.dart';
import 'package:get/get.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import '../../data/NewsEntity.dart';
import '../../theme/app_boxDecoration.dart';
import '../../theme/app_colors.dart';
import '../../utils/AppUtils.dart';
import 'evaluate_record_controller.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class evaluate_record_page2 extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return EvaluateListState();
  }
}

class EvaluateListState extends State<evaluate_record_page2>
    with TickerProviderStateMixin {
  var controller = Get.put(evaluateRecordController());

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    // _tabController.dispose();
    super.dispose();
  }

  TabController? tab;

  @override
  Widget build(BuildContext context) {
    return Obx(() {
      if (tab == null && controller.typeList.length > 0) {
        tab = TabController(length: controller.typeList.length, vsync: this)
          ..addListener(() {
            print('VideoListState${tab?.index}');
            // controller.evaluateList.assignAll([]);
          });
        ;
      }
      if (tab == null) {
        return SizedBox();
      } else {
        return Scaffold(
          appBar: AppBar(
            elevation: 0,
            centerTitle: true,
            leading: IconButton(
                icon: Icon(
                  Icons.arrow_back_ios,
                  color: Colors.white,
                  size: 22,
                ),
                onPressed: () => Navigator.pop(context)),
            title: Text(
              "每日评价",
              style: TextStyle(fontSize: 16),
            ),
            bottom: PreferredSize(
              preferredSize:
                  Size.fromHeight(controller.articles.length > 0 ? 250 : 45),
              child: Material(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                        margin: EdgeInsets.only(left: 10, right: 10, top: 10),
                        color: Colors.white,
                        width: double.infinity,
                        child: firstEvaluate()),
                    TabBar(
                      tabs: controller.typeList.map((e) {
                        return Tab(
                          text: e.typeName,
                        );
                      }).toList(),
                      unselectedLabelColor: const Color(0xffacb3bf),
                      indicatorColor: c_f12f38,
                      labelColor: Colors.black,
                      indicatorWeight: 2.0,
                      indicatorSize: TabBarIndicatorSize.label,
                      //底部高度
                      indicatorPadding: EdgeInsets.all(10),
                      controller: tab,
                      isScrollable: true,
                      // Tabbar是否可水平滚动,false
                      physics: BouncingScrollPhysics(),
                      dragStartBehavior:
                          DragStartBehavior.start, // 默认start 更为平滑
                    ),
                  ],
                ),
              ),
            ),
          ),
          body: TabBarView(controller: tab, children: EvaluateFragPages()),
        );
      }
    });
  }

  List<Widget> EvaluateFragPages() {
    List<Widget> pages = [];
    for (int i = 0; i < controller.typeList.length; i++) {
      RefreshController refreshController =
          RefreshController(initialRefresh: true);
      pages.add(Container(
        width: double.infinity,
        child: RefreshConfiguration(
          headerBuilder: () => ClassicHeader(),
          footerBuilder: () => ClassicFooter(),
          child: SmartRefresher(
            controller: refreshController,
            enablePullDown: true,
            enablePullUp: true,
            onRefresh: () {
              controller.page = 1;
              controller.reqNewsList(i, refreshController);
            },
            onLoading: () {
              controller.page += 1;
              controller.reqNewsList(i, refreshController);
            },
            child: Obx(() {
              return Container(
                margin: EdgeInsets.all(10),
                decoration: r10WhiteDecoration,
                child: ListView.separated(
                    physics: NeverScrollableScrollPhysics(),
                    separatorBuilder: (context, index) {
                      return Container(
                          margin: EdgeInsets.symmetric(horizontal: 15),
                          child: Divider(height: 1));
                    },
                    itemBuilder: (context, index) {
                      return newsItem(controller.evaluateList[index]);
                    },
                    itemCount: controller.evaluateList.length,
                    shrinkWrap: true),
              );
            }),
          ),
        ),
      ));
    }
    return pages;
  }

  Widget newsItem(ListNewsElement item) {
    return InkWell(
      onTap: () {
        controller.clickRecordDetail(item);
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 15, horizontal: 10),
        decoration: BoxDecoration(
            color: Colors.white, borderRadius: BorderRadius.circular(10)),
        child: Row(children: [
          Expanded(
            flex: 2,
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Text(
                item.title ?? "",
                style: TextStyle(
                    fontSize: 15, fontWeight: FontWeight.bold, color: c_333333),
              ),
              SizedBox(height: 13),
              Row(
                children: [
                  Text(
                    AppUtils.dateToString(item.updatedAt),
                    style: TextStyle(fontSize: 12, color: c_999999),
                  ),
                  SizedBox(width: 10),
                  Icon(
                    Icons.remove_red_eye_outlined,
                    size: 12,
                    color: c_999999,
                  ),
                  Text(item.viewNum.toString(),
                      style: TextStyle(fontSize: 12, color: c_999999)),
                ],
              )
            ]),
          ),
          SizedBox(
            height: 72,
            width: 108,
            child: ClipRRect(
              child: Image.network(
                item.imgHead ?? "https://via.placeholder.com/108x72",
                fit: BoxFit.fill,
              ),
              borderRadius: BorderRadius.circular(10),
            ),
          )
        ]),
      ),
    );
  }

  Widget firstEvaluate() {
    print('firstVideo${controller.articles.length}');
    if (controller.articles.length < 1) {
      return SizedBox(height: 0);
    }

    return Container(
      margin: EdgeInsets.all(10),
      width: double.infinity,
      height: 160,
      child: Swiper(
        itemBuilder: (context, index) {
          var item = controller.articles[index];
          return InkWell(
            onTap: () {
              controller.clickRecordDetail(controller.articles[index]);
            },
            child: Column(children: [
              InkWell(
                onTap: () {
                  controller.clickRecordDetail(item);
                },
                child: Stack(
                  alignment: Alignment.center,
                  children: [
                    ClipRRect(
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(10),
                          topRight: Radius.circular(10)),
                      child: Image.network(
                        item.imgHead ?? "https://via.placeholder.com/108x92",
                        fit: BoxFit.fill,
                        width: double.infinity,
                        height: 120,
                      ),
                    ),
                    // SvgPicture.asset(R.assetsImgIcSvgPlay)
                  ],
                ),
              ),
              Container(
                width: double.infinity,
                alignment: Alignment.centerLeft,
                padding:
                    const EdgeInsets.symmetric(vertical: 10, horizontal: 10),
                decoration: const BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(10),
                        bottomRight: Radius.circular(10))),
                child: Text(
                  item.title ?? "",
                  maxLines: 1,
                  style: TextStyle(color: c_333333),
                ),
              )
            ]),
          );
        },
        itemCount: controller.articles.length,
        pagination: SwiperPagination(
            builder: DotSwiperPaginationBuilder(
                color: Colors.black26,
                activeColor: c_f12f38,
                size: 5,
                activeSize: 7)),
        scale: 0.9,
      ),
    );
  }
}
